package com.rescue.controller.user;

import com.rescue.annotation.GlobalInterceptor;
import com.rescue.enumeration.OperationFrequencyType;
import com.rescue.forum.dto.SaveCommentDTO;
import com.rescue.forum.dto.UserCommentQueryDTO;
import com.rescue.result.PageResult;
import com.rescue.result.Result;
import com.rescue.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user/forum/comment")
@RestController("userCommentController")
@Api(tags = "客户端评论相关接口")
@Slf4j
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 根据文章Id获取评论
     * @param userCommentQueryDTO
     * @return
     */
    @ApiOperation("根据文章Id获取评论")
    @GetMapping("/getByArticleId")
    public Result<PageResult> getByArticleId(UserCommentQueryDTO userCommentQueryDTO){
        log.info("根据文章Id获取评论列表{}",userCommentQueryDTO.getArticleId());
        PageResult pageResult = commentService.userGetCommentList(userCommentQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 发布评论
     * @param saveCommentDTO
     * @return
     */
    @ApiOperation("发布评论")
    @PostMapping("/save")
    @GlobalInterceptor(frequency = OperationFrequencyType.POST_COMMENT)
    public Result save(@RequestBody SaveCommentDTO saveCommentDTO){
        log.info("发布评论{}",saveCommentDTO);
        commentService.save(saveCommentDTO);
        return Result.success();
    }

    /**
     * 评论置顶取消置顶
     * @param commentId
     * @param topType
     * @return
     */
    @ApiOperation("评论置顶取消置顶")
    @PostMapping("/top")
    public Result topComment(Integer commentId,Integer topType){
        log.info("评论置顶取消置顶");
        commentService.top(commentId,topType);
        return Result.success();
    }

    /**
     * 评论点赞/取消
     * @param commentId
     * @param status
     * @return
     */
    @ApiOperation("评论点赞/取消")
    @PostMapping("/doLike")
    @GlobalInterceptor(frequency = OperationFrequencyType.DO_LIKE)
    public Result doLike(Integer commentId,Integer status){
        log.info("评论点赞/取消");
        commentService.doLike(commentId,status);
        return Result.success();
    }

    /**
     * 用户删除评论
     * @param commentId
     * @return
     */
    @ApiOperation("用户删除评论")
    @DeleteMapping("/delete")
    public Result delete(Integer commentId){
        log.info("用户删除评论{}",commentId);
        commentService.userDeleteById(commentId);
        return Result.success();
    }
}
